﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebEFDemo
{
    public partial class Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            //实例化数据库访问上下文
            EFDemoUserListEntities db = new EFDemoUserListEntities();
            Student stu = new Student()
            {
                //添加实体的属性
                stuname = txt_name.Text.Trim(),
                stuage = Convert.ToInt32(txt_age.Text.Trim()),
                phone = txt_phone.Text.Trim(),
                email = txt_email.Text.Trim(),
                classid = Convert.ToInt32(dropclass.SelectedValue)
            };
            db.Student.Add(stu);
            //SaveChanges
            int i = db.SaveChanges();
            if (i > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功！');location.href='Index.aspx';</script>");
            }
            else
            {
                Response.Write("添加失败！");
            }
        }
    }
}